//
//  WelcomeView.m
//  MobileGameLiving2
//
//  Created by reflection on 15/9/6.
//  Copyright (c) 2015年 李旭东. All rights reserved.
//

#import "WelcomeView.h"

@interface WelcomeView(){
    NSInteger _pageCount;
    NSInteger _currentPageNumber;
    CGFloat _move_x;
    FRPCustomScrollView_SelectType _selectType;
    BOOL isTouchesBegan;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *arrViews;

@end

@implementation WelcomeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initPageViewWithFrame:(CGRect)frame views:(NSArray *)views selectType:(FRPCustomScrollView_SelectType)selectType{
    self = [super initWithFrame:frame];
    if (self) {
        _selectType = selectType;
        _arrViews = views;
        _pageCount = views.count;
        _currentPageNumber = 0;
        _move_x = 0.0;
        self.userInteractionEnabled = YES;
        [self setMainView];
    }
    return self;
}

- (void)setMainView{
    UIView *viewBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 395, 275)];
    [self addSubview:viewBG];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 395, 275)];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.userInteractionEnabled=YES;
    if (_pageCount > 1) {
        [_scrollView setContentSize:CGSizeMake(395 * 3, 275)];
        [_scrollView setContentOffset:CGPointMake(395, 0) animated:NO];
        _move_x = 395;
    }else{
        [_scrollView setContentSize:CGSizeMake(395, 275)];
    }
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
//    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(ScrollTimer) userInfo:nil repeats:YES];
    //pageController
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(175, 245, 30, 10)];
    _pageControl = pageControl;
    _pageControl.numberOfPages = _pageCount;
    _pageControl.tintColor = [UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPage = _currentPageNumber;
    [_pageControl addTarget:self action:@selector(pageNumberChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
    
    if (_pageCount > 1) {
        [self reSetScrollViewOffSet];
    }
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:singleTap];
}

- (void)reSetScrollViewOffSet{
    for (UIView *view in _scrollView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat left = 0;
    for (NSInteger i = _currentPageNumber - 1 ; i < _currentPageNumber + 2; i++) {
        NSInteger index = i;
        if (index < 0) {
            index = _pageCount - 1;
        }
        
        if (index == _pageCount) {
            index = 0;
        }
        
        UIView *view = [_arrViews objectAtIndex:index];
        [view setFrame:CGRectMake(left, 0, 395, 275)];
        [_scrollView addSubview:view];
        left += self.frame.size.width;
    }
    [_scrollView setContentOffset:CGPointMake(395, 0) animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if ( scrollView.contentOffset.x != _move_x && (scrollView.contentOffset.x == self.frame.size.width*2 || scrollView.contentOffset.x == 0)) {
        if (scrollView.contentOffset.x == 0) {
            _currentPageNumber--;
        }else{
            _currentPageNumber++;
        }
        
        if (_currentPageNumber == _pageCount) {
            _currentPageNumber = 0;
        }
        
        if (_currentPageNumber == -1) {
            _currentPageNumber = _pageCount -1;
        }
        
        [_pageControl setCurrentPage:_currentPageNumber];
        
        
        [self reSetScrollViewOffSet];
    }
}

- (void)pageNumberChanged:(UIPageControl *)pageControl{
    if (pageControl.currentPage > _currentPageNumber) {
        [_scrollView setContentOffset: CGPointMake(395 * 2, 0.0f) animated:YES];
        [self performSelector:@selector(scrollViewDidEndDecelerating:) withObject:_scrollView afterDelay:0.5];
    }else if (pageControl.currentPage < _currentPageNumber){
        [_scrollView setContentOffset: CGPointMake(0.0f, 0.0f) animated:YES];
        [self performSelector:@selector(scrollViewDidEndDecelerating:) withObject:_scrollView afterDelay:0.5];
    }
}

//- (void)ScrollTimer{
//    int timeCounter;
//    timeCounter++;
//    if(timeCounter == 4){
//        timeCounter = 1;
//        [_scrollView setContentOffset:CGPointMake(0, 0)];
//    }
//    [_scrollView setContentOffset:CGPointMake(395*timeCounter, 0)];
//}

- (void)singleTap:(UITapGestureRecognizer *)tapGesture{
    if (_selectType != FRPCustomScrollView_SelectType_CanSelected) {
        return;
    }
    
    if (_pageViewDelegate) {
        [_pageViewDelegate pageViewDidClickedWithPageNumber:_currentPageNumber];
    }
}

@end
